package com.humanmonth.home.component.page.htree
{
	import com.humanmonth.home.component.page.htree.layout.HorizenlLayout;
	import com.humanmonth.home.component.page.htree.layout.Layout;
	import com.humanmonth.home.component.page.htree.layout.VerticalLayout;
	import com.humanmonth.home.component.page.htree.remote.TreeToXml;
	import com.humanmonth.home.component.page.signature.remote.Connector;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.events.FlexEvent;
	
	public class MyControlBarAs extends MyControlBar
	{
		private var connector:Connector=new Connector();
		public function MyControlBarAs()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,myInit);
		}
		
		private function myInit(event:Event):void{
			this.saveXml.addEventListener(MouseEvent.CLICK,onSaveXml);
			this.myLayout.addEventListener(Event.CHANGE,changeLayout);
			this.savePic.addEventListener(MouseEvent.CLICK,savePicToLocal);
			this.downloadPic.addEventListener(MouseEvent.CLICK,connector.downloadFile)
		}
		
		private function savePicToLocal(event:Event):void{
			var board:WhiteBoardAs=Application.application.hTree.whiteBoard;
			var myData:BitmapData=new BitmapData(board.width,board.height);
			myData.draw(board);
			connector.savePic(myData,enableDownload);		
		}
		
		private function enableDownload(event:Event):void{
			this.downloadPic.enabled=true;
		}
		
		/**
		 * 改变布局
		 */
		private function changeLayout(event:Event):void{
			if(this.myLayout.selectedItem.label=="垂直布局"){
				Layout.isVertical=true;
				new VerticalLayout().layout();
			}else{
				Layout.isVertical=false;
				new HorizenlLayout().layout();
			}
		}
		
		private function onSaveXml(event:Event):void{
			Alert.show( new TreeToXml().buildXml());
		}
		
	}
}